package com.xzc.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzc.auth.service.SysRoleService;
import com.xzc.common.config.exception.XzcException;
import com.xzc.common.result.Result;
import com.xzc.model.system.SysRole;
import com.xzc.vo.system.AssginRoleVo;
import com.xzc.vo.system.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author 徐生
 * @version 1.0
 * Date 2024/3/23 20:30
 * Description: SysRoleController
 */
@RestController
@RequestMapping("/admin/system/sysRole")
@Api(tags = "角色管理接口")
@Slf4j
public class SysRoleController {
    //http:localhost:8800/admin/system/sysRole/findAll

    //注入service
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 查询所有角色
     * @return
     */
    /*@GetMapping("/findAll")
    public List<SysRole> findAll(){
        //调用service方法实现
        List<SysRole> list = sysRoleService.list();
        return list;
    }*/
    @GetMapping("/findAll")
    @ApiOperation("查询所有的角色")
    public Result findAll(){
        //调用service方法实现
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }

    //条件分页查询
    //page：当前页  limit：每页显示记录数
    //SysRoleQueryVo：条件对象
    @GetMapping("/{page}/{limit}")
    @ApiOperation("条件分页查询")
    public Result pageQueryRole(@PathVariable("page") Long page,
                                @PathVariable("limit") Long limit,
                                SysRoleQueryVo sysRoleQueryVo){
        log.info("条件分页查询：{}",sysRoleQueryVo);
        //调用service的方法实现
        //1、创建Page对象，传递分页相关参数
        Page<SysRole> pageParam = new Page<>(page, limit);
        //2、封装条件，判断条件是否为空，不为空进行封装
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(sysRoleQueryVo.getRoleName())) {
            //不为null，进行封装（模糊查询）
            wrapper.like(SysRole::getRoleName,sysRoleQueryVo.getRoleName());
        }
        //3、调用方法实现
        Page<SysRole> pageModel = sysRoleService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }


    /**
     * 添加角色
     * @param sysRole
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("添加角色")
    public Result save(@RequestBody SysRole sysRole){
        log.info("添加角色，角色信息：{}",sysRole);
        boolean is_success = sysRoleService.save(sysRole);
        if (is_success) return Result.ok();
        return Result.fail();
    }

    /**
     * 根据id先查询，然后再进行修改
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ApiOperation("根据id查询")
    public Result getById(@PathVariable Long id){
        log.info("查询角色id：{}",id);
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }

    /**
     * 修改角色
     * @param sysRole
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改角色")
    public Result update(@RequestBody SysRole sysRole){//以前的提交少了@RequestBody会报错，现已该好
        log.info("修改角色信息：{}",sysRole);
        boolean is_success = sysRoleService.updateById(sysRole);
        if (is_success) return Result.ok();
        return Result.fail();
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除角色")
    public Result deleteById(@PathVariable Long id){
        log.info("删除角色id：{}",id);
        boolean is_success = sysRoleService.removeById(id);
        if (is_success) return Result.ok();
        return Result.fail();
    }

    /**
     * 批量删除
     * 前端传来的数组格式：[1,2,3]
     * @param idList
     * @return
     */
    @DeleteMapping("/batchRemove")
    @ApiOperation("批量删除")
    public Result deleteBatch(@RequestBody List<Long> idList){
        log.info("批量删除id：{}",idList);
        return sysRoleService.removeByIds(idList) ? Result.ok() : Result.fail();
    }

    //查询所有角色 和 当前用户所属角色
    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String, Object> roleMap = sysRoleService.findRoleByAdminId(userId);
        return Result.ok(roleMap);
    }

    //为用户分配角色
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }
}
